package com.edu.controller;

import com.edu.dao.IndexDao;
import com.edu.dao.UserDao;

import com.edu.data.User;
import com.edu.data.ShoppingCart;
import com.edu.util.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    IndexDao dao;

    @Autowired
    UserDao userDao;

    @RequestMapping("/logins")
    public String logins(String userId, String password, String code, String mess, Model model, HttpServletRequest req) throws SQLException {
        String rightcode = (String) req.getSession().getAttribute("numrand");// 汉字验证码的话则是hanzirand
        Validator validator = new Validator(Validator.toSingleParameters(req));

        if (code != null && rightcode != null && !rightcode.equals(code)) {
            model.addAttribute("userId", userId);
            model.addAttribute("password", password);
            validator.addError("mess", "验证码错误，请重新输入!");
        } else if (!validator.checkRequiredError(new String[]{"userId", "password"})) {
            User user = userDao.findById(userId);
            if (user == null)
                validator.addError("userId", "用户不存在");
            else {
                if (user.getEuPassword().equals(password)) {
                    userDao.setLogin(user, true);//设置当前为登录状态
                    req.getSession().setAttribute("loginUser", user);
                    model.addAttribute("loginUser", user);
                } else {
                    validator.addError("password", "密码不正确");
                }
            }
        }

        if (!validator.hasErrors()) {
            ShoppingCart cart = (ShoppingCart) req.getSession().getAttribute("cart");
            if (cart != null && cart.items != null && cart.items.size() > 0) {// 先购物后登录
//                resp.sendRedirect(req.getContextPath() + "/Cart");
            } else {// 尚未购物
                return "forward:/index/indexs";
            }
        } else {
            req.setAttribute("errors", validator.getErrors());
            model.addAttribute("errors", validator.getErrors());
            return "../../login";
        }
        return null;
    }

    @RequestMapping("/cart")
    public String cart(HttpServletRequest req) {
        User user=(User)req.getSession().getAttribute("loginUser");
        userDao.setLogin(user, false);//设置当前为非登录状态
        req.getSession().removeAttribute("loginUser");
        // 清除购物车
        req.getSession().removeAttribute("cart");
        req.getSession().removeAttribute("cart2");
        //清除最近浏览
        //req.getSession().removeAttribute("recentViewProducts");
        return "redirect:/index/indexs";
    }

    @RequestMapping("/register")
    public String register(User user,HttpServletRequest req) throws SQLException {
        Validator validator = new Validator(Validator.toSingleParameters(req));
        try {
            user.setEuStatus(User.USER_TYPE_ORDINARY);
            Boolean sex=user.getEuSex().equals("male");
            String Sex=sex ? "T" : "F";
            user.setEuSex(Sex);
            user.setEuLogin(Float.parseFloat("0"));
            System.out.println("user:"+user);
            userDao.save(user);
        } catch (Exception e) {
            validator.addError("userId", "用户名字已经存在");
        }
        if (!validator.hasErrors()) {
            req.setAttribute("msg", "已经成功注册请登录");
            return "../../message";
        } else {
            req.setAttribute("errors", validator.getErrors());
            return "../../register";
        }
    }

}
